package calculator_52;

import java.util.Stack;

public class UserMainCode
{
	public static int output1;
	
	static class N {
		Object v;
		N l, r;
		public N(Object v) {
			this.v = v;
		}
		public int c() {
			if(l != null) {
				Character c = (Character)v;
				switch(c) {
				case '*':
					return l.c() * r.c();
				case '+':
					return l.c() + r.c();
				case '-':
					return l.c() - r.c();
				}
			}
			return (Integer)v;
		}
		public String toString() {
			StringBuffer b = new StringBuffer();
			if(l != null)
				b.append("(").append(l);
			b.append(v);
			if(r != null)
				b.append(r).append(")");
			return b.toString();
		}
	}
	
	public static void CalC(String s)
	{
		s = "(" + s.substring(1, s.length() - 1) + ")";
		Stack<Character> q = new Stack<Character>();
		Stack<N> l = new Stack<N>();
		boolean b = false;
		int n = 0;
		char p;
		for(char c : s.toCharArray()) {
			if(c >= '0' && c <= '9') {
				b = true;
				n = n * 10 + c - '0';
			} else {
				if(b) {
					l.push(new N(n));
					n = 0;
					b = false;
				}
				if(c == ')') {
					while((p = q.pop()) != '(') {
						N m = new N(p);
						m.r = l.pop();
						m.l = l.pop();
						l.push(m);
					}
				} else
					q.push(c);
			}
		}
		output1 = l.pop().c();
	}
	
	public static void main(String[] args) {
		CalC("{((2+8)*9)-(5*(5+2))}");
		System.out.println(output1);
	}
}
